import $path from "./ServerPath.js"
export default httpRequest;
import $constant from "./constant.js"

export function httpRequest(url, method, params) {
	return new Promise((resolve, reject) => {
		try {
			const token = uni.getStorageSync($constant.API_TOKEN);
			const header = {
				'mobile_Authorization' : "Bearer " + token
			}
			uni.request({
				url: $path.prefix + url,
				method: method,
				header: header,
				data: params,
				dataType: 'json',
				success(res) {
					if(res.data.code === 200) {
						resolve(res.data);
						return
					}else if(res.data.code === 401) {
						/* 未登录 */
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}else if(res.data.code === 406) {
						uni.login({
							provider: "weixin",
							success: (res) => {
								httpRequest("/front/store/openid", "post", {wechatCode: res.code});
							}
						})
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: "error"
						})
					}
				}
			})
		}catch(e) {
			console.log(e);
		}
	})
}